{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "existing-politics",
   "metadata": {},
   "source": [
    "# 数据操作\n",
    "\n",
    "视频地址：https://www.bilibili.com/video/BV1CV411Y7i4?spm_id_from=333.999.0.0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "frozen-little",
   "metadata": {},
   "source": [
    "## 导入torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "greek-digit",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "invalid-audience",
   "metadata": {},
   "source": [
    "## 生成一个一维张量\n",
    ">`torch.arange(起点, 终点, step=相邻点间隔大小)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "sticky-beach",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])\n"
     ]
    }
   ],
   "source": [
    "# 张量X\n",
    "x = torch.arange(0,12,1)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "elegant-concert",
   "metadata": {},
   "source": [
    "## 自定义张量赋值\n",
    ">自定义张量：`torch.tensor([ [],[] ])`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "overhead-performance",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "灰度图像 3*3图像 格式 [行[列]]\n",
      "tensor([[1, 2, 3],\n",
      "        [4, 5, 6],\n",
      "        [7, 8, 9]])\n",
      "彩色图像 2*3 图像 格式 [行[列[灰度值]]]\n",
      "tensor([[[1, 2, 3],\n",
      "         [4, 5, 6],\n",
      "         [7, 8, 9]],\n",
      "\n",
      "        [[4, 5, 6],\n",
      "         [4, 5, 6],\n",
      "         [4, 5, 6]]])\n"
     ]
    }
   ],
   "source": [
    "# 灰度图像 3*3图像 格式 [行[列]]\n",
    "a = torch.tensor([\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "])\n",
    "print(\"灰度图像 3*3图像 格式 [行[列]]\")\n",
    "print(a)\n",
    "\n",
    "\n",
    "# 彩色图像 2*3 图像 格式 [行[列[灰度值]]]\n",
    "b = torch.tensor([\n",
    "    [[1,2,3],[4,5,6],[7,8,9]],\n",
    "    [[4,5,6],[4,5,6],[4,5,6]]\n",
    "])\n",
    "print(\"彩色图像 2*3 图像 格式 [行[列[灰度值]]]\")\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hourly-midwest",
   "metadata": {},
   "source": [
    "## 查询张量的形状和元素的总数\n",
    ">查询张量形状：`张量变量.shape`  \n",
    ">查询元素总数：`张量变量.numel()`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "automotive-serum",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张量形状\n",
      "torch.Size([12])\n",
      "张量元素总个数\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "x = torch.arange(0,12,1)\n",
    "\n",
    "# 张量形状\n",
    "print(\"张量形状\")\n",
    "print(x.shape)\n",
    "\n",
    "# 张量元素个数\n",
    "print(\"张量元素总个数\")\n",
    "print(x.numel())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "polar-smoke",
   "metadata": {},
   "source": [
    "## 改变一个张量形状，不改变其元素数量和元素值\n",
    ">改变张量形状：`张量变量.reshape(第一层元素数量, 第二层元素数量, 第三层元素数量)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "talented-savage",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])\n",
      "tensor([[[ 0,  1,  2,  3],\n",
      "         [ 4,  5,  6,  7],\n",
      "         [ 8,  9, 10, 11]]])\n",
      "tensor([[ 0,  1,  2,  3],\n",
      "        [ 4,  5,  6,  7],\n",
      "        [ 8,  9, 10, 11]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.arange(0,12,1)\n",
    "print(x)\n",
    "\n",
    "# 把1维张量x转变为三维张量\n",
    "X = x.reshape(1,3,4)\n",
    "print(X)\n",
    "\n",
    "# 把1维张量x转变为二维张量，三行四列\n",
    "X = x.reshape(3,4)\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "subsequent-julian",
   "metadata": {},
   "source": [
    "## 张量连接 \n",
    ">把维度相同的张量合并（维度不增加）：`torch.cat((张量1,张量2),dim=0)` dim=0增加行，dim=1增加列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "scientific-boutique",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n",
      "tensor([[2, 1, 4, 3],\n",
      "        [1, 2, 3, 4],\n",
      "        [4, 3, 2, 1]])\n",
      "张量连接dim=0 增加行\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [ 2.,  1.,  4.,  3.],\n",
      "        [ 1.,  2.,  3.,  4.],\n",
      "        [ 4.,  3.,  2.,  1.]])\n",
      "张量连接dim=0 增加列\n",
      "tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],\n",
      "        [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]])\n"
     ]
    }
   ],
   "source": [
    "X = torch.arange(12, dtype=torch.float32).reshape(3,4)\n",
    "print(X)\n",
    "\n",
    "Y = torch.tensor([\n",
    "    [2, 1, 4, 3],\n",
    "    [1, 2, 3, 4],\n",
    "    [4, 3, 2, 1]\n",
    "])\n",
    "print(Y)\n",
    "\n",
    "# 张量连接dim=0 增加行\n",
    "print('张量连接dim=0 增加行')\n",
    "print(torch.cat((X,Y),dim=0))\n",
    "\n",
    "# 张量连接dim=1 增加列\n",
    "print('张量连接dim=0 增加列')\n",
    "print(torch.cat((X,Y),dim=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "solid-smell",
   "metadata": {},
   "source": [
    "## numpy张量与torch张量转换\n",
    ">torch转numpy：`张量变量.numpy()`  \n",
    ">numpy转torch：`torch.tensor(张量变量)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "exact-philip",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2, 1, 4, 3],\n",
      "        [1, 2, 3, 4],\n",
      "        [4, 3, 2, 1]])\n",
      "[[2 1 4 3]\n",
      " [1 2 3 4]\n",
      " [4 3 2 1]]\n",
      "tensor([[2, 1, 4, 3],\n",
      "        [1, 2, 3, 4],\n",
      "        [4, 3, 2, 1]])\n"
     ]
    }
   ],
   "source": [
    "X = torch.tensor([\n",
    "    [2, 1, 4, 3],\n",
    "    [1, 2, 3, 4],\n",
    "    [4, 3, 2, 1]\n",
    "])\n",
    "print(X)\n",
    "\n",
    "# 把torch转为numpy\n",
    "Y = X.numpy()\n",
    "print(Y)\n",
    "\n",
    "# 把numpy转为torch\n",
    "Z = torch.tensor(Y)\n",
    "print(Z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "successful-virgin",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
